<template>
  <div class="menu-container">
    <div class="menu-header">
      <div class="menu-categories">
        <h2>美味菜品</h2>
        <el-radio-group v-model="currentCategory" size="medium">
          <el-radio-button label="all">全部</el-radio-button>
          <el-radio-button v-for="category in categories" :key="category" :label="category">
            {{ category }}
          </el-radio-button>
        </el-radio-group>
        <div class="category-description" v-if="currentCategory !== 'all'">
          <span>{{ getCategoryDescription(currentCategory) }}</span>
        </div>
      </div>
      <el-input
        placeholder="搜索菜品..."
        prefix-icon="el-icon-search"
        v-model="searchQuery"
        class="search-input"
      ></el-input>
    </div>
    <el-row :gutter="20">
      <el-col :xs="24" :sm="12" :md="8" :lg="6" v-for="dish in displayDishes" :key="dish.id">
        <el-card :body-style="{ padding: '0px' }" class="dish-card" :class="{ 'adding': addingToCart === dish.id }">
          <div class="dish-image-container">
            <img :src="dish.image" class="dish-image">
            <div class="category-tag">{{ dish.category }}</div>
            <div class="dish-actions">
              <el-button 
                type="primary" 
                size="small"
                @click="addToCart(dish)"
                :loading="addingToCart === dish.id"
                class="add-cart-btn"
              >
                <i class="el-icon-shopping-cart-2"></i>
                <span>加入购物车</span>
              </el-button>
            </div>
          </div>
          <div class="dish-content">
            <h3>{{ dish.name }}</h3>
            <p class="dish-description">{{ dish.description }}</p>
            <div class="bottom">
              <span class="price">¥{{ dish.price }}</span>
              <span class="sales-count">月售 {{ dish.sales || 0 }}</span>
            </div>
          </div>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script>
export default {
  name: 'DishMenu',
  data() {
    return {
      searchQuery: '',
      currentCategory: 'all',
      addingToCart: null,
      categoryDescriptions: {
        '川菜': '麻辣鲜香，令人回味',
        '粤菜': '清淡鲜美，突出本味',
        '江浙菜': '清鲜淡雅，精工细作',
        '湘菜': '香辣��口，色泽美观',
        '特色小吃': '精致美味，独具特色'
      }
    }
  },
  computed: {
    dishes() {
      return this.$store.state.dishes
    },
    categories() {
      return [...new Set(this.dishes.map(dish => dish.category))]
    },
    filteredDishes() {
      if (!this.searchQuery) return this.dishes
      const query = this.searchQuery.toLowerCase()
      return this.dishes.filter(dish => 
        dish.name.toLowerCase().includes(query) ||
        dish.description.toLowerCase().includes(query)
      )
    },
    displayDishes() {
      let dishes = this.filteredDishes
      if (this.currentCategory !== 'all') {
        dishes = dishes.filter(dish => dish.category === this.currentCategory)
      }
      return dishes
    }
  },
  methods: {
    addToCart(dish) {
      this.addingToCart = dish.id
      this.$store.commit('addToCart', dish)
      this.$message({
        message: '已添加到购物车',
        type: 'success',
        duration: 2000,
        customClass: 'custom-message'
      })
      setTimeout(() => {
        this.addingToCart = null
      }, 300)
    },
    getCategoryDescription(category) {
      return this.categoryDescriptions[category] || ''
    }
  }
}
</script>

<style scoped>
.menu-container {
  padding: 20px;
  max-width: 1200px;
  margin: 0 auto;
  animation: fadeIn 0.5s ease;
}
.menu-header {
  margin-bottom: 30px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.menu-categories {
  display: flex;
  flex-direction: column;
  gap: 15px;
}
.menu-header h2 {
  font-size: 24px;
  color: #303133;
  margin: 0;
}
.search-input {
  width: 300px;
}
.dish-card {
  margin-bottom: 20px;
  transition: all 0.3s;
  border-radius: 15px;
  overflow: hidden;
  transform: translateY(0);
}
.dish-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 12px 24px rgba(0,0,0,0.15);
}
.dish-image-container {
  height: 200px;
  overflow: hidden;
  position: relative;
}
.dish-image {
  width: 100%;
  height: 200px;
  object-fit: cover;
  transition: transform 0.3s;
}
.dish-card:hover .dish-image {
  transform: scale(1.05);
}
.dish-description {
  color: #666;
  margin: 10px 0;
  height: 40px;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}
.bottom {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 13px;
}
.price {
  font-size: 18px;
  color: #f56c6c;
  font-weight: bold;
}
.el-button {
  border-radius: 20px;
  padding: 8px 20px;
}
.el-button i {
  margin-right: 5px;
}
.category-tag {
  position: absolute;
  top: 10px;
  right: 10px;
  background-color: rgba(0, 0, 0, 0.6);
  color: white;
  padding: 4px 8px;
  border-radius: 4px;
  font-size: 12px;
}
.category-description {
  font-size: 14px;
  color: #909399;
  margin-top: 5px;
}
.dish-actions {
  position: absolute;
  right: 10px;
  bottom: 10px;
  opacity: 0;
  transition: opacity 0.3s;
}
.dish-card:hover .dish-actions {
  opacity: 1;
}
.dish-content {
  padding: 14px;
}
.sales-count {
  font-size: 12px;
  color: #909399;
}
.adding {
  animation: shake 0.5s ease-in-out;
}
@keyframes shake {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-5px); }
  75% { transform: translateX(5px); }
}
@media screen and (max-width: 768px) {
  .menu-header {
    flex-direction: column;
    gap: 15px;
  }
  .menu-title, .search-input {
    width: 100%;
  }
  .el-radio-group {
    display: flex;
    overflow-x: auto;
    padding-bottom: 5px;
  }
}
.add-cart-btn {
  background-color: #409EFF;
  border: none;
  padding: 8px 15px;
  border-radius: 20px;
  display: flex;
  align-items: center;
  gap: 5px;
}
.add-cart-btn:hover {
  background-color: #66b1ff;
}
.add-cart-btn i {
  margin: 0;
  font-size: 16px;
}
.add-cart-btn span {
  font-size: 14px;
}
@keyframes fadeIn {
  from { opacity: 0; transform: translateY(20px); }
  to { opacity: 1; transform: translateY(0); }
}
</style> 